import Destoriable from "./destoriable.js";
import Block from "./block.js";
import Direction from "./direction.js";
import config from "./gameconfig.js";
class Moveable extends Destoriable {
    constructor(x, y, imgSrc, width, height, speed = 100, dir = Direction.TOP) {
        super(x, y, imgSrc, width, height);
        this.next = new Block();
        this.maxX = config.MapMaxWidth - this.width;
        this.maxY = config.MapMaxHeight - this.height;
        this.speed = speed;
        this.direction = dir;
    }
    move(cols) {
        Object.assign(this.next, this);
        var step = this.speed / 1000 * config.GameInterval;
        switch (this.direction) {
            case Direction.TOP:
                this.next.y -= step;
                break;
            case Direction.LEFT:
                this.next.x -= step;
                break;
            case Direction.RIGHT:
                this.next.x += step;
                break;
            case Direction.BOTTOM:
                this.next.y += step;
                break;
        }
        this.next.x = this.next.x <= 0 ? 0 : this.next.x;
        this.next.y = this.next.y <= 0 ? 0 : this.next.y;
        this.next.x = this.next.x >= this.maxX ? this.maxX : this.next.x;
        this.next.y = this.next.y >= this.maxY ? this.maxY : this.next.y;
        var canMove = cols.find(e => this.next.collision(e));
        if (!canMove) {
            Object.assign(this, this.next);
        }
        return !!canMove;
    }
}
export default Moveable;
